package com.gemptc.service.iphone;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.gemptc.bean.Photo_Library;
import com.gemptc.bean.Travelsbean;
import com.gemptc.daoimp.InsertImp;
import com.gemptc.daoimp.SelectImp;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

/**
 * Servlet implementation class jieshou
 */
@WebServlet("/jieshou")
public class jieshou extends HttpServlet {

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public jieshou() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	private static final long serialVersionUID = 1L;
	int status = 0;
	boolean a;
	JSONObject jsonObject = new JSONObject();

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter writer = response.getWriter();
		// 初始化SmartUpload框架，用来下载图片等
		SmartUpload smartUpload = new SmartUpload();
		try {
			// 固定yuju
			smartUpload.initialize(this.getServletConfig(), request, response);
			smartUpload.upload();
			// 得到手机端传来的t_tittle
			String t_tittle = smartUpload.getRequest().getParameter("t_title");
			// 得到手机端传来的t_content
			String t_content = smartUpload.getRequest().getParameter(
					"t_content");
			String user_id=smartUpload.getRequest().getParameter("user_id");
			// 得到上传的t_time
			Date currentTime = new Date();
			String t_time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
					.format(currentTime);
			// 得到手机端传来的t_hidden
			String st = smartUpload.getRequest().getParameter("flag");
			System.out.println(st+"传过来的是否隐藏");
			if (st.equals("true")) {
				a = true;
				
			} else {
				a = false;
			}
			boolean t_hidden = a;
			// 得到user_id,以判断是哪个用户发表的
			// 输出这几个字段 看是否已经得到手机端的传值
			System.out.println(t_tittle + "--->" + t_content + "--->" + t_time
					+ "--->" + t_hidden+"--->" +user_id);

			Travelsbean travelsbean = new Travelsbean(Integer.parseInt(user_id), t_tittle,
					t_time, t_content, t_hidden);
			System.out.println(travelsbean);
			// 把手机端除了图片以外的所有属性插入到数据库
			InsertImp impTravels = new InsertImp();
			travelsbean = impTravels.addTravels(travelsbean);
			
			SelectImp selectImp=new SelectImp();
			
			int t_id=selectImp.selectT_id(t_time);
			
			// 接受手机端传过来的图片
			List<Photo_Library> imgs=new ArrayList<Photo_Library>();
			com.jspsmart.upload.Files files = smartUpload.getFiles();
			for (int i = 0; i < files.getCount(); i++) {
				com.jspsmart.upload.File file = files.getFile(i);
				if (!file.isMissing()) {
					SimpleDateFormat sdf = new SimpleDateFormat(
							"yyyyMMddHHmmssSSS");
					String pl_path = sdf.format(new java.util.Date());
					pl_path = pl_path + "." + file.getFileExt();// 得到文件的扩展名
					/*String filename =this.getServletContext().getRealPath("/")
							+ "img\\" + pl_path;*/
					String filename ="D:\\workspace\\Jinnang7\\WebContent\\img\\" + pl_path;
					System.out.println("文件路径：" + filename);
					file.saveAs(filename);					
					imgs.add(new Photo_Library(Integer.parseInt(user_id),t_id, pl_path));
					
				}
			}
			//System.out.println(imgs);
			if (impTravels.addPhoto_Library(imgs)) {
				//System.out.println("商品图片上传成功");
			}else {
				//System.out.println("商品图片上传失败");
			}
		} catch (SmartUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

	}

	

	private void printJson(HttpServletResponse response) {
		PrintWriter pw = null;
		try {
			pw = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(jsonObject.toString());
		pw.write(jsonObject.toString());
		pw.close();
	}

}
